package run.microservice.stock.controller;

import com.fasterxml.jackson.databind.JsonNode;
import com.tictactec.ta.lib.Core;
import com.tictactec.ta.lib.MInteger;
import com.tictactec.ta.lib.RetCode;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import run.microservice.stock.dao.DictionaryDAO;
import run.microservice.stock.model.dto.TestDTO;
import run.microservice.stock.model.entity.Dictionary;
import run.microservice.stock.utils.ObjectMapperUtil;

import java.time.LocalDate;
import java.util.List;

/**
 * @author lunfy
 * @description:
 * @date 2024-04-22 14:49
 */
@Slf4j
@RestController
@RequestMapping("test")
public class TestController {

    private double[] input = new double[200];
    private int[] inputInt = new int[200];
    private double[] output = new double[200];
    private int[] outputInt = new int[200];
    private MInteger outBegIdx = new MInteger();
    private MInteger outNbElement = new MInteger();

    /**
     * The number of periods to average together.
     * 含义：20日移动平均
     */
    public static final int PERIODS_AVERAGE = 20;

    /** 历史收盘价 */
    public static double[] hisClose = new double[]{91.5, 94.815, 94.375, 95.095, 93.78, 94.625, 92.53, 92.75, 90.315, 92.47, 96.125, 97.25, 98.5, 89.87
            };

    @Resource
    private DictionaryDAO dictionaryDAO;

    /**
     * 接口示例
     * @return 数据信息
     */
    @GetMapping("/json")
    public List<Dictionary> test(@RequestParam(value = "queryDate", required = false) LocalDate queryDate){
        log.info("调用测试");
        String str = "{'number':123,'string':'sample data','number string':'8143661439548533232','array':[1,2,3,{'value':'8143661439548533232n','type':'Big Number'},'8143661439548533232'],'safe integer':9007199254740991,'unsafe integer':{'value':'9007199254111741001n','type':'Big Number'},'object':{'complex':[40.66,-73.23,{'value':'-73.9899789999999000000009n','type':'Big Number'}],'type':'Point'}}";

        JsonNode objNode = ObjectMapperUtil.getInstance().toJsonNode(str);
        JsonNode arrayNode = objNode.get("array");
        this.test1();

        if (arrayNode.isArray()) {
            String[] args = new String[]{"abc", "def", "xyz"};
            // throw new BusinessException(ResultEnum.TEST_ERROR, args);
        }
        TestDTO entity = new TestDTO();
        entity.setGender("1");

        Dictionary dictionary = new Dictionary();
        dictionary.setDictCode("test1");
        dictionary.setDictName("测试1");
        dictionaryDAO.save(dictionary);

        // IKeyGenerator
//        IKeyGenerator keyGenerator = KeyGeneratorFactory.getKeyGenerator("");
//        keyGenerator.generate(null, "");
        return dictionaryDAO.list();
    }


    private void test1(){
        Core core = new Core();

        // 假设我们有这些输入数据（实际应用中应该是你的市场数据）
        //参数准备
        //收盘价的数组
        double[] closePrice = hisClose;
        //移动平均线数值的数组
        double[] out = new double[closePrice.length];
        //MInteger类: mutable integer，可变整数
        MInteger begin = new MInteger();
        MInteger length = new MInteger();

        // 2只乌鸦
        RetCode retCode = core.cdl2Crows(0, closePrice.length - 1, closePrice, closePrice, closePrice, closePrice, begin, length,  outputInt);

        //调用核心类
//        RetCode retCode = core.sma(
//                //int startIdx：开始的索引
//                0,
//                //int endIdx：结束的索引
//                closePrice.length - 1,
//                //收盘价集合
//                closePrice,
//                //移动平均的周期数，如 MA20
//                PERIODS_AVERAGE,
//                //如 MA20 线开始的索引值
//                begin,
//                //如 MA20 线的长度
//                length,
//                //当日的 MA20 移动平均线的数值
//                out
//        );

        //打印信息
        if (retCode == RetCode.Success) {
            //数值上 = PERIODS_AVERAGE-1
            System.out.println("输出开始的周期数: " + begin.value);
            //总周期数
            System.out.println("输出结束的周期数: " + (begin.value + length.value - 1));

            //遍历有线
            for (int i = begin.value; i < begin.value + length.value; i++) {
                //检验当前记录对应的id
                Long id = Long.valueOf(hisClose.length);
                System.out.println("当前记录的id: "+id);
                //当日收盘价（保留两位小数）
                String tempClose = String.format("%.2f", closePrice[i]);
                //移动平均数（保留两位小数）
                String tempMovAver = String.format("%.2f", out[i - begin.value]);
                //拼接字符串
                String line = "Period # " +
                        i +
                        " 当日收盘价 = " +
                        tempClose +
                        " 移动平均数 = " +
                        tempMovAver;
                System.out.println(line);
            }
        }
        else {
            System.out.println("Error");
        }
    }

    public static void main(String[] args) {
        TestController testController = new TestController();
        testController.test1();
    }
}
